package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.MemberBrandAttention;
import com.genesis.mall.entity.PmsBrand;
import com.genesis.mall.entity.UmsMember;
import com.genesis.mall.mapper.MemberBrandAttentionMapper;
import com.genesis.mall.service.IMemberBrandAttentionService;
import com.genesis.mall.service.IPmsBrandService;
import com.genesis.mall.service.IUmsMemberService;
import com.genesis.mall.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author: HShuai
 * @Date: 2022/08/03/18:31星期三
 * @Description:
 */
@Service
public class MemberBrandAttentionServiceImpl extends ServiceImpl<MemberBrandAttentionMapper, MemberBrandAttention> implements IMemberBrandAttentionService {

    @Autowired
    private IUmsMemberService umsMemberService;

    @Autowired
    private IPmsBrandService brandService;

    @Override
    public void saveBrand(MemberBrandAttention memberBrandAttention) {
        UmsMember umsMember = umsMemberService.getById(memberBrandAttention.getMemberId());
        PmsBrand pmsBrand = brandService.getById(memberBrandAttention.getBrandId());
        memberBrandAttention.setBrandName(pmsBrand.getName());
        memberBrandAttention.setLogo(pmsBrand.getLogo());
        memberBrandAttention.setCreateTime(LocalDateTime.now());
        memberBrandAttention.setMemberNickname(umsMember.getNickname());
        save(memberBrandAttention);
    }

    @Override
    public void clear(HttpServletRequest request) {
        //获取用户id
        String token = request.getHeader("Authorization");
        String token1 = token.substring(6);
        Claims claims = TokenUtil.getClaims(token1);
        Long memberId = Long.parseLong(claims.get("umsMember").toString()) ;

        LambdaQueryWrapper<MemberBrandAttention> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberBrandAttention::getMemberId,memberId);
        //清空当前用户关注的品牌列表
        remove(lqw);
    }

    @Override
    public MemberBrandAttention getDetailById(Long brandId, HttpServletRequest request) {

        //获取用户id
        String token = request.getHeader("Authorization");
        String token1 = token.substring(6);
        Claims claims = TokenUtil.getClaims(token1);
        Long memberId = Long.parseLong(claims.get("umsMember").toString()) ;
        //进行条件查询   当前用户id
        LambdaQueryWrapper<MemberBrandAttention> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberBrandAttention::getBrandId, brandId);
        lqw.eq(MemberBrandAttention::getMemberId,memberId);
            return getOne(lqw);
    }

    @Override
    public PageDto<MemberBrandAttention> pageList(HttpServletRequest request, Integer pageNum, Integer pageSize) {
        //获取用户id
        String token = request.getHeader("Authorization");
        String token1 = token.substring(6);
        Claims claims = TokenUtil.getClaims(token1);
        Long memberId = Long.parseLong(claims.get("umsMember").toString()) ;

        //创建分页构造器
        Page<MemberBrandAttention> memberBrandAttentionPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<MemberBrandAttention> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberBrandAttention::getMemberId,memberId);
        page(memberBrandAttentionPage,lqw);
        PageDto<MemberBrandAttention> pageDto = new PageDto<>();
        pageDto.setPageNum(pageNum);
        pageDto.setPageSize(pageSize);
        pageDto.setTotal((int) memberBrandAttentionPage.getTotal());
        pageDto.setTotalPage((int) memberBrandAttentionPage.getPages());
        pageDto.setList(memberBrandAttentionPage.getRecords());
        return pageDto;
    }

}
